package com.ocom.company.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.master.EntityWechatPerInfo;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.request.wx.UnBindRequest;
import com.ocom.common.utils.StringUtils;
import com.ocom.common.vo.company.GzhPerInfoVo;
import com.ocom.company.dao.IDaoWechatPerInfo;
import com.ocom.company.service.WechatPerInfoService;
import com.ocom.mysql.aspect.DataSourceAspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;


@Service
public class WechatperInfoServiceImpl extends ServiceImpl<IDaoWechatPerInfo, EntityWechatPerInfo> implements WechatPerInfoService {

    @Autowired
    private IDaoWechatPerInfo daoWechatPerInfo;
    /**
     * 获取id
     * @return
     */
    @DataSourceAspect(type = DataSourceEnum.MAIN_DB)
    public List<Long> getCompanyIdsByOpenid(String openid){
        QueryWrapper<EntityWechatPerInfo> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("openid",openid);
        List<EntityWechatPerInfo> list=this.list(queryWrapper);
        if(!list.isEmpty() && list.size()>0){
            List<Long> comIds= list.stream().map(EntityWechatPerInfo -> EntityWechatPerInfo.getComId()).collect(Collectors.toList());
            return comIds;
        }
        return  null;
    }

    @DataSourceAspect(type = DataSourceEnum.MAIN_DB)
    @Override
    public EntityWechatPerInfo saveWxUser(EntityWechatPerInfo entityWechatPerInfo) {
        this.save(entityWechatPerInfo);
        return entityWechatPerInfo;
    }

    @DataSourceAspect(type = DataSourceEnum.MAIN_DB)
    @Override
    public EntityWechatPerInfo getWxUserByPid(Long companyId, Long pId) {
        QueryWrapper<EntityWechatPerInfo> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("com_id",companyId);
        queryWrapper.eq("p_id",pId);
        queryWrapper.last("limit 1");
        return this.getOne(queryWrapper);
    }

    //查找appid、com_id、p_id匹配的记录
    @DataSourceAspect(type = DataSourceEnum.MAIN_DB)
    @Override
    public EntityWechatPerInfo getWechatPerInfo(Long companyId, Long userId, String appid) {
        QueryWrapper<EntityWechatPerInfo> query = Wrappers.query();
        if (StringUtils.isNotNull(companyId)){
            query.eq("com_id",companyId);
        }
        if (StringUtils.isNotNull(userId)){
            query.eq("p_id",userId);
        }
        if (StringUtils.isNotEmpty(appid)){
            query.eq("appid",appid);
        }
        query.last("limit 1");
        return daoWechatPerInfo.selectOne(query);
    }

    //根据传参条件查询表 wechat_per_info 得到集合 list1 ；
    @DataSourceAspect(type = DataSourceEnum.MAIN_DB)
    @Override
    public List<EntityWechatPerInfo> getWechatPerInfo(String openid) {
        QueryWrapper<EntityWechatPerInfo> query = Wrappers.query();
        query.eq("openid",openid);
        query.orderByDesc("update_time");
        return daoWechatPerInfo.selectList(query);

    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.MAIN_DB)
    public List<GzhPerInfoVo> getWechatPerList(String openid, String appid) {
        return daoWechatPerInfo.getCompanyListByAgentGzh(openid,appid);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.MAIN_DB)
    public boolean delWxUser(Long companyId, UnBindRequest unBindRequest) {
        UpdateWrapper<EntityWechatPerInfo> update = Wrappers.update();
        update.eq("com_id",companyId);
        update.eq("appid",unBindRequest.getAppid());
        update.eq("p_id",unBindRequest.getPId());
        update.eq("openid",unBindRequest.getOpenid());
        return this.remove(update);
    }
}
